﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ASBCC.Business;
using BEGDC.CC.BL.Workflow;
using System.Configuration;
using System.Web.Services;
using BEGDC.CC.BL.Common.Query;
using BEGDC.CC.BL.UserManagement;
using BEGDC.Utility.Castle;
using BEGDC.CC.BL.Common;
using System.Data;

namespace BEGDC.CC.Web
{
    public partial class FrameHeader : System.Web.UI.Page
    {

        //protected System.Web.UI.HtmlControls.HtmlGenericControl DelegateUsersLI;

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
                BindDelegateUser();
        }

        protected override void OnInit(EventArgs e)
        {
            this.DelegateUsers.SelectedIndexChanged += new EventHandler(DelegateUsers_SelectedIndexChanged);
            base.OnInit(e);
        }

        protected string LogonUserDisplayName
        {
            get
            {
                return ((this.User as CSPrincipal).Identity as CCIdentity).ActualUserInfo.NameCN;
            }
        }

        protected bool IsShowUserGuide
        {
            get
            {
                return bool.Parse(ConfigurationManager.AppSettings["IsShowUserGuide"]);
            }
        }

        private void BindDelegateUser()
        {
            IUser userService = AppContainer.Instance[ServiceConst.UserService] as IUser;
            string currentUserName = (User as CSPrincipal).CIL;
            string currentUserNo = userService.GetUser(currentUserName).Id;
            //bind all delegate user//
            //1.clear dropdownlist
            DelegateUsers.Items.Clear();
            //2.add self restore
            DelegateUsers.Items.Add(new ListItem("Restore", currentUserName));
            //3.get users who delegate role to the current user and add into list
            foreach(DataRow dr in userService.GetDelegateUserByCurrentUser(currentUserNo).Rows)
            {
                //value is employeeNo
                DelegateUsers.Items.Add(new ListItem("As " + dr["nameen"].ToString(), dr["employeeNo"].ToString()));
            }
            //3.5 if only Restore hide DelegateUsers
            if (DelegateUsers.Items.Count < 2)
            {
                DelegateUsersLI.Visible = false;
            }
            //4.select current delegate user depend on sesson["AsWho"]
            else if (Session["AsWho"] != null)
            {
                ListItem item = DelegateUsers.Items.FindByValue(Session["AsWho"].ToString());
                if(item != null)
                    DelegateUsers.SelectedValue = item.Value;
            }

        }

        protected void DelegateUsers_SelectedIndexChanged(object sender, EventArgs e)
        {
            //set sesson["AsWho"] depend on DelegateUsers.SelectedValue
            if(DelegateUsers.SelectedValue == (User as CSPrincipal).CIL)
                Session["AsWho"] = null;
            else
                Session["AsWho"] = DelegateUsers.SelectedValue;
            MainMenu.DataBind();
        }


        [WebMethod]
        public static string GetAcceptState()
        {
            if (HttpContext.Current.Session["agreement-accept"] != null && HttpContext.Current.Session["agreement-accept"].ToString() == "Accept")
                return "Accept";
            else
                return "UnAccept";
        }
    }
}
